import ajax, { ListResult } from '../ajax';

export function getList(data: any) {
  return ajax.post<ListResult>(`/ControlPanels/Users/Query`, data);
}

export function updateEntity(data: any) {
  if (!data.application) {
    data.applicationId = 10;
  }

  if (data.userId) {
    return ajax.put<any>('/ControlPanels/Users', data);
  } else {
    return ajax.post<any>('/ControlPanels/Users', data);
  }
}

export function deleteEntity(id: number) {
  let deleteCommand = { id: id };
  return ajax.put<any>('/ControlPanels/Users/Actions/Delete', deleteCommand);
}

export function getUserInfo(userId: number) {
  return ajax.get<any>(`/ControlPanels/Users/${userId}`);
}

export function getRoles(userId: number) {
  return ajax.get<Array<any>>(`/ControlPanels/Users/${userId}/Roles`);
}

export function bindRole(userId: number, roleId: number) {
  let data = {
    userId,
    roleId,
  };
  return ajax.post(`/ControlPanels/Users/Actions/Bind/Role`, data);
}

export function unbindRole(userId: number, roleId: number) {
  let data = {
    userId,
    roleId,
  };
  return ajax.post(`/ControlPanels/Users/Actions/Unbind/Role`, data);
}

export function getGroups(userId: number) {
  return ajax.get<Array<any>>(`/ControlPanels/Users/${userId}/Groups`);
}

export function bindGroup(userId: number, groupId: number) {
  let data = {
    userId,
    groupId,
  };
  return ajax.post(`/ControlPanels/Groups/Actions/Bind/User`, data);
}

export function unbindGroup(userId: number, groupId: number) {
  let data = {
    userId,
    groupId,
  };
  return ajax.post(`/ControlPanels/Groups/Actions/Unbind/User`, data);
}

export function resetPassword(userId: number) {
  let resetCommand = { applicationId: 10, userId: userId };
  return ajax.put<any>(
    '/ControlPanels/Users/Actions/Reset/Password',
    resetCommand
  );
}

export function getUsersByName(name: string) {
  if (name) {
    return ajax.get(`/ControlPanels/Applications/100/Users/Name/${name}`);
  } else {
    return ajax.get(`/ControlPanels/Applications/100/Users/Available`);
  }
}

export function getSalesConsultants() {
  return ajax.get(`/ControlPanels/Users/SalesConsultants`);
}
